<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>手机号登录</h1>
      <!-- 用户登录表单 -->
      <el-card class="login-card" shadow="never">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
          <el-form-item prop="mobile">
            <el-input type="text" v-model="loginForm.mobile" placeholder="请输入手机号"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">
              用户平台使用协议
            </el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width:450px" @click="goLogin">登录</el-button>

          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'login',
  data() {
    return {
      //表单对象
      loginForm: {
        //登录手机号
        mobile: '13800000002',
        //密码
        password: 'hm#qd@23!',
        //是否同意协议
        isAgree: true
      },
      //表单验证规则
      loginFormRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[356789]\d{9}$/, message: "手机号不合法，请重新输入", trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码要在6~20位之间', trigger: 'blur' }
        ],
        //required必填只能验证null,'',undefined场景，不能处理boolean
        isAgree: [
          {
            validator: (rule, value, callback) => {
              console.log('value:', value)
              value ? callback() : callback(new Error('您必须勾选用户的使用协议'))
            }
          }
        ]
      }
    }
  },
  methods: {
    //去登录
    goLogin() {
      this.$refs.loginFormRef.validate(async isOk => {
        console.log('isOk:', isOk)
        //调用登录接口
        //通过vuex的actions进行登录操作
        await this.$store.dispatch('user/login', this.loginForm)
        //跳转到首页
        this.$router.push('/')

      })
    }
  }
}
</script>

<style lang="scss"></style>

<style scoped lang="scss">
.login-container {
  display: flex;
}

.logo {
  flex: 3;
  height: 100vh;
  background: url(../../assets/common/login_back.png) no-repeat;
  background-size: cover;
}

.form {
  flex: 2;
  padding-left: 176px;
  display: flex;
  flex-direction: column;
  justify-content: center;

  h1 {
    padding-left: 20px;
    font-size: 24px;
  }

  .login-card {
    border: none
  }
}


//vue3 样式穿透 :deep(选择器)  {....}
//vue2 样式穿透 ::v-deep 选择器 {....}
::v-deep .el-input__inner {
  background: #f4f5fb;
}
</style>